package com.xh.rufongwallpaper.common.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xh.rufongwallpaper.entity.Category;
import com.xh.rufongwallpaper.entity.DownloadRecord;
import com.xh.rufongwallpaper.entity.Wallpaper;
import com.xh.rufongwallpaper.vo.CategoryVO;
import com.xh.rufongwallpaper.vo.DownloadRecordVO;
import com.xh.rufongwallpaper.vo.PageResponseVO;
import com.xh.rufongwallpaper.vo.WallpaperVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * VO转换工具类
 */
public class VOConverter {

    /**
     * 将Wallpaper实体转换为WallpaperVO
     */
    public static WallpaperVO convertToWallpaperVO(Wallpaper wallpaper) {
        if (wallpaper == null) {
            return null;
        }
        
        WallpaperVO vo = new WallpaperVO();
        vo.setId(wallpaper.getId());
        vo.setTitle(wallpaper.getTitle());
        vo.setDescription(wallpaper.getDescription());
        vo.setCategory(wallpaper.getCategoryId().toString()); // 前端需要字符串形式
        vo.setFilePath(wallpaper.getFilePath());
        vo.setThumbnailPath(wallpaper.getThumbnailPath());
        // 预览图使用缩略图路径
        vo.setThumbnailPathForPreview(wallpaper.getThumbnailPath());
        vo.setWidth(wallpaper.getWidth());
        vo.setHeight(wallpaper.getHeight());
        vo.setFileSize(wallpaper.getFileSize());
        vo.setDownloads(wallpaper.getDownloadCount());
        vo.setCreateTime(wallpaper.getCreateTime());
        
        // 解析标签
        if (wallpaper.getTags() != null && !wallpaper.getTags().isEmpty()) {
            List<String> tags = List.of(wallpaper.getTags().split(","));
            vo.setTags(tags);
        }
        
        return vo;
    }
    
    /**
     * 将Wallpaper实体集合转换为WallpaperVO集合
     */
    public static List<WallpaperVO> convertToWallpaperVOList(List<Wallpaper> wallpapers) {
        return wallpapers.stream()
                .map(VOConverter::convertToWallpaperVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将Page<Wallpaper>转换为PageResponseVO<WallpaperVO>
     */
    public static PageResponseVO<WallpaperVO> convertToPageResponse(Page<Wallpaper> page) {
        List<WallpaperVO> voList = convertToWallpaperVOList(page.getRecords());
        return PageResponseVO.of(voList, (int) page.getCurrent() - 1, (int) page.getSize(), page.getTotal());
    }
    
    /**
     * 将Category实体转换为CategoryVO
     */
    public static CategoryVO convertToCategoryVO(Category category, Integer wallpaperCount) {
        if (category == null) {
            return null;
        }
        
        CategoryVO vo = new CategoryVO();
        vo.setId(category.getId());
        vo.setName(category.getName());
        vo.setDescription(category.getDescription());
        vo.setIcon(category.getIcon());
        vo.setWallpaperCount(wallpaperCount);
        
        return vo;
    }
    
    /**
     * 将Category实体集合转换为CategoryVO集合
     */
    public static List<CategoryVO> convertToCategoryVOList(List<Category> categories, 
                                                           List<Integer> wallpaperCounts) {
        List<CategoryVO> voList = new java.util.ArrayList<>();
        for (int i = 0; i < categories.size(); i++) {
            voList.add(convertToCategoryVO(categories.get(i), 
                    i < wallpaperCounts.size() ? wallpaperCounts.get(i) : 0));
        }
        return voList;
    }
    
    /**
     * 将DownloadRecord实体转换为DownloadRecordVO
     */
    public static DownloadRecordVO convertToDownloadRecordVO(DownloadRecord downloadRecord) {
        if (downloadRecord == null) {
            return null;
        }
        
        DownloadRecordVO vo = new DownloadRecordVO();
        vo.setId(downloadRecord.getId());
        vo.setDeviceId(downloadRecord.getDeviceId());
        vo.setWallpaperId(downloadRecord.getWallpaperId());
        vo.setCreateTime(downloadRecord.getCreateTime());
        
        // 如果关联了壁纸对象，则转换
        if (downloadRecord.getWallpaper() != null) {
            vo.setWallpaper(convertToWallpaperVO(downloadRecord.getWallpaper()));
        }
        
        return vo;
    }
    
    /**
     * 将DownloadRecord实体集合转换为DownloadRecordVO集合
     */
    public static List<DownloadRecordVO> convertToDownloadRecordVOList(List<DownloadRecord> downloadRecords) {
        return downloadRecords.stream()
                .map(VOConverter::convertToDownloadRecordVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将Page<DownloadRecord>转换为PageResponseVO<DownloadRecordVO>
     */
    public static PageResponseVO<DownloadRecordVO> convertToDownloadRecordPageResponse(Page<DownloadRecord> page) {
        List<DownloadRecordVO> voList = convertToDownloadRecordVOList(page.getRecords());
        return PageResponseVO.of(voList, (int) page.getCurrent() - 1, (int) page.getSize(), page.getTotal());
    }
} 